//
//  Spirit.cpp
//  petgame
//
//  Created by zz on 2018/12/12.
//

#include "Spirit.hpp"


Spirit*Spirit::create(PROGRESSTYPE type,int value,Vec2 pos){
    
    //创建spirit对象
    auto spirit=new Spirit();
    if(spirit&&spirit&&spirit->init(type,value,pos)){
        //自动释放对象
        spirit->autorelease();
        return spirit;
    }else{
        
        CC_SAFE_DELETE(spirit);
        return NULL;
        
    }

}
bool Spirit::init(PROGRESSTYPE type,int value,Vec2 pos){

    //记录progressbar 的类型
    this->_type=type;
    //选择需要创建的progressbar 的类型
    this->selectProgress(type);
    //设置progressbar 的坐标
    this->setPosition(pos);

    return true;
}


void Spirit::selectProgress(PROGRESSTYPE type){
    
    
    switch(type){
       
        case PROGRESSTYPE::SPIRIT:{
            //初始化progress的布局，不同的类型有着不同的背景贴图和状态显示，和不同的坐标信息
            this->sp=Sprite::create("k1.png");
            this->sp->setScale(0.4);
            this->sp->setPosition(Point(10,0));
            this->addChild(this->sp,3);
            auto line=Sprite::create("pline3.png");
            line->setScale(0.5);
            line->setPosition(Vec2(45,0));
            this->addChild(line);
            auto lbody=Sprite::create("pb2s.png");
            lbody->setScale(0.5);
            this->prog=ProgressTimer::create(lbody);
            this->prog->setType(ProgressTimer::Type::BAR);
            this->prog->setPosition(Vec2(50,0));
            this->prog->setMidpoint(Point(0,0));
            this->prog->setBarChangeRate(Vec2(1, 0));
            this->prog->setPercentage(100);
            this->prog->setScale(0.4);
            this->addChild(this->prog,2);
        }
            break;
        case PROGRESSTYPE::HAPPY:{
            
            this->sp=Sprite::create("k2.png");
            this->sp->setScale(0.4);
            this->addChild(this->sp,3);
            this->sp->setPosition(Point(10,0));
            auto line=Sprite::create("pline3.png");
            line->setScale(0.5);
            line->setPosition(Vec2(45,0));
            this->addChild(line);
            auto lbody=Sprite::create("pb2.png");
            lbody->setScale(0.5);
            this->prog=ProgressTimer::create(lbody);
            this->prog->setType(ProgressTimer::Type::BAR);
            this->prog->setPosition(Vec2(50,0));
            this->prog->setMidpoint(Point(0,0));
            this->prog->setBarChangeRate(Vec2(1, 0));
            this->prog->setPercentage(50);
            this->prog->setScale(0.4);
            this->addChild(this->prog,2);
            
            
        }
            break;
        case PROGRESSTYPE::HUNGER:{
            
            this->sp=Sprite::create("k3.png");
            this->sp->setScale(0.4);
            this->sp->setPosition(Point(10,0));
            this->addChild(this->sp,3);
            auto line=Sprite::create("pline3.png");
            line->setScale(0.5);
            line->setPosition(Vec2(45,0));
            this->addChild(line);
            auto lbody=Sprite::create("pb3.png");
            lbody->setScale(0.5);
            this->prog=ProgressTimer::create(lbody);
            this->prog->setType(ProgressTimer::Type::BAR);
            this->prog->setPosition(Vec2(50,0));
            this->prog->setMidpoint(Point(0,0));
            this->prog->setBarChangeRate(Vec2(1, 0));
            this->prog->setPercentage(50);
            this->prog->setScale(0.4);
            this->addChild(this->prog,2);
        }
            break;
    }
}

void Spirit::toless(){
     //修改progressbar的值
     //检查当前progressbar的值是否超出0.0f
    float offset=this->prog->getPercentage()-5;
    if(offset>=0){
    this->prog->setPercentage(offset);
    }else{
        this->prog->setPercentage(0.0f);
    }
}

void Spirit::toadd(int value){
    
     //修改progressbar的值
    //检查当前progressbar的值是否超出100.0f
    if(this->prog->getPercentage()+value>=100.0f){
        this->prog->setPercentage(100.0f);
    }else{
    
        this->prog->setPercentage(this->prog->getPercentage()+value);
    }
}

float Spirit::getValue(){
    //获取progressbar的值
    return this->prog->getPercentage();
}

void Spirit::setValue(float v){
     //设置progressbar的值
    this->prog->setPercentage(v);
}
